package com.savi.myapplication.fragment;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.icu.util.Calendar;
import android.net.Uri;
import android.os.Bundle;

import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;

import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.savi.myapplication.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class FuelCardFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    static String pdfFilePath = null;

    // 初始化其他EditText
    private Button buttonSign;
    private EditText fuel_line1_1;
    private EditText fuel_line1_2;
    private EditText fuel_line2_1;
    private EditText fuel_line2_2;
    private EditText fuel_line2_3;
    private EditText fuel_line3_1;
    private EditText fuel_line3_2;
    private EditText fuel_line3_3;
    private EditText fuel_line3_4;
    private EditText fuel_line3_5;
    private EditText fuel_line3_6;
    private EditText fuel_line4_1;
    private EditText fuel_line4_2;
    private EditText fuel_line4_3;
    private EditText fuel_line4_4;
    private EditText fuel_line4_5;
    private EditText fuel_line4_6;
    private EditText fuel_form1_1;
    private EditText fuel_form1_2;
    private EditText fuel_form1_3;
    private EditText fuel_form1_4;
    private EditText fuel_form2_1;
    private EditText fuel_form2_2;
    private EditText fuel_form2_3;
    private EditText fuel_form2_4;
    private EditText fuel_form3_1;
    private EditText fuel_form3_2;
    private EditText fuel_form3_3;
    private EditText fuel_form3_4;
    private EditText fuel_form4_1;
    private EditText fuel_form4_2;
    private EditText fuel_form4_3;
    private EditText fuel_form4_4;
    private EditText fuel_form5_1;
    private EditText fuel_form5_2;
    private EditText fuel_form5_3;
    private EditText fuel_form5_4;
    private EditText fuel_form6_1;
    private EditText fuel_form6_2;
    private EditText fuel_form6_3;
    private EditText fuel_form6_4;
    private EditText fuel_form7_1;
    private EditText fuel_form7_2;
    private EditText fuel_form7_3;
    private EditText fuel_form7_4;
    private EditText fuel_form8_1;
    private EditText fuel_form8_2;
    private EditText fuel_form8_3;
    private EditText fuel_form8_4;
    private EditText fuel_form9_1;
    private EditText fuel_form9_2;
    private EditText fuel_form9_3;
    private EditText fuel_form9_4;
    private EditText fuel_form10_1;
    private EditText fuel_form10_2;
    private EditText fuel_form10_3;
    private EditText fuel_form10_4;
    private EditText fuel_form11_1;
    private EditText fuel_form11_2;
    private EditText fuel_form11_3;
    private EditText fuel_form11_4;
    private EditText fuel_form12_1;
    private EditText fuel_form12_2;
    private EditText fuel_form12_3;
    private EditText fuel_form12_4;
    private EditText fuel_form13_1;
    private EditText fuel_form13_2;
    private EditText fuel_form13_3;
    private EditText fuel_form13_4;
    private EditText fuel_form14_1;
    private EditText fuel_form14_2;
    private EditText fuel_form14_3;
    private EditText fuel_form14_4;
    private EditText fuel_form15_1;
    private EditText fuel_form15_2;
    private EditText fuel_form15_3;
    private EditText fuel_form15_4;
    private EditText fuel_form16_1;
    private EditText fuel_form16_2;
    private EditText fuel_form16_3;
    private EditText fuel_form16_4;
    private EditText fuel_form17_1;
    private EditText fuel_form17_2;
    private EditText fuel_form17_3;
    private EditText fuel_form17_4;
    private EditText fuel_form18_1;
    private EditText fuel_form18_2;
    private EditText fuel_form18_3;
    private EditText fuel_form18_4;
    private EditText fuel_form19_1;
    private EditText fuel_form19_2;
    private EditText fuel_form19_3;
    private EditText fuel_form19_4;
    private EditText fuel_form20_1;
    private EditText fuel_form20_2;
    private EditText fuel_form20_3;
    private EditText fuel_form20_4;
    private EditText fuel_form21_1;
    private EditText fuel_form21_2;
    private EditText fuel_form21_3;
    private EditText fuel_form21_4;
    private EditText fuel_form22_1;
    private EditText fuel_form22_2;
    private EditText fuel_form22_3;
    private EditText fuel_form22_4;
    private EditText fuel_form23_1;
    private EditText fuel_form23_2;
    private EditText fuel_form23_3;
    private EditText fuel_form23_4;
    private EditText fuel_form24_1;
    private EditText fuel_form24_2;
    private EditText fuel_form24_3;
    private EditText fuel_form24_4;
    private EditText fuel_form25_1;
    private EditText fuel_form25_2;
    private EditText fuel_form25_3;
    private EditText fuel_form25_4;
    private EditText fuel_form26_1;
    private EditText fuel_form26_2;
    private EditText fuel_form26_3;
    private EditText fuel_form26_4;
    private EditText fuel_form27_1;
    private EditText fuel_form27_2;
    private EditText fuel_form27_3;
    private EditText fuel_form27_4;
    private EditText fuel_form28_1;
    private EditText fuel_form28_2;
    private EditText fuel_form28_3;
    private EditText fuel_form28_4;
    private Button buttonGeneratePDF;

    private  Bitmap signatureBitmap;


    public FuelCardFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FuelCardFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FuelCardFragment newInstance(String param1, String param2) {
        FuelCardFragment fragment = new FuelCardFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fuel_card, container, false);

        buttonSign = view.findViewById(R.id.fuel_line1_3_button);
        // 初始化其他EditText
        fuel_line1_1 = view.findViewById(R.id.fuel_line1_1);
        fuel_line1_2 = view.findViewById(R.id.fuel_line1_2);
//        fuel_line1_3 = view.findViewById(R.id.fuel_line1_3_button);
        fuel_line2_1 = view.findViewById(R.id.fuel_line2_1);
        fuel_line2_2 = view.findViewById(R.id.fuel_line2_2);
        fuel_line2_3 = view.findViewById(R.id.fuel_line2_3);
        fuel_line3_1 = view.findViewById(R.id.fuel_line3_1);
        fuel_line3_2 = view.findViewById(R.id.fuel_line3_2);
        fuel_line3_3 = view.findViewById(R.id.fuel_line3_3);
        fuel_line3_4 = view.findViewById(R.id.fuel_line3_4);
        fuel_line3_5 = view.findViewById(R.id.fuel_line3_5);
        fuel_line3_6 = view.findViewById(R.id.fuel_line3_6);
        fuel_line4_1 = view.findViewById(R.id.fuel_line4_1);
        fuel_line4_2 = view.findViewById(R.id.fuel_line4_2);
        fuel_line4_3 = view.findViewById(R.id.fuel_line4_3);
        fuel_line4_4 = view.findViewById(R.id.fuel_line4_4);
        fuel_line4_5 = view.findViewById(R.id.fuel_line4_5);
        fuel_line4_6 = view.findViewById(R.id.fuel_line4_6);

        fuel_form1_1 = view.findViewById(R.id.fuel_form1_1);
        fuel_form1_2 = view.findViewById(R.id.fuel_form1_2);
        fuel_form1_3 = view.findViewById(R.id.fuel_form1_3);
        fuel_form1_4 = view.findViewById(R.id.fuel_form1_4);
        fuel_form2_1 = view.findViewById(R.id.fuel_form2_1);
        fuel_form2_2 = view.findViewById(R.id.fuel_form2_2);
        fuel_form2_3 = view.findViewById(R.id.fuel_form2_3);
        fuel_form2_4 = view.findViewById(R.id.fuel_form2_4);
        fuel_form3_1 = view.findViewById(R.id.fuel_form3_1);
        fuel_form3_2 = view.findViewById(R.id.fuel_form3_2);
        fuel_form3_3 = view.findViewById(R.id.fuel_form3_3);
        fuel_form3_4 = view.findViewById(R.id.fuel_form3_4);
        fuel_form4_1 = view.findViewById(R.id.fuel_form4_1);
        fuel_form4_2 = view.findViewById(R.id.fuel_form4_2);
        fuel_form4_3 = view.findViewById(R.id.fuel_form4_3);
        fuel_form4_4 = view.findViewById(R.id.fuel_form4_4);
        fuel_form5_1 = view.findViewById(R.id.fuel_form5_1);
        fuel_form5_2 = view.findViewById(R.id.fuel_form5_2);
        fuel_form5_3 = view.findViewById(R.id.fuel_form5_3);
        fuel_form5_4 = view.findViewById(R.id.fuel_form5_4);
        fuel_form6_1 = view.findViewById(R.id.fuel_form6_1);
        fuel_form6_2 = view.findViewById(R.id.fuel_form6_2);
        fuel_form6_3 = view.findViewById(R.id.fuel_form6_3);
        fuel_form6_4 = view.findViewById(R.id.fuel_form6_4);
        fuel_form7_1 = view.findViewById(R.id.fuel_form7_1);
        fuel_form7_2 = view.findViewById(R.id.fuel_form7_2);
        fuel_form7_3 = view.findViewById(R.id.fuel_form7_3);
        fuel_form7_4 = view.findViewById(R.id.fuel_form7_4);
        fuel_form8_1 = view.findViewById(R.id.fuel_form8_1);
        fuel_form8_2 = view.findViewById(R.id.fuel_form8_2);
        fuel_form8_3 = view.findViewById(R.id.fuel_form8_3);
        fuel_form8_4 = view.findViewById(R.id.fuel_form8_4);
        fuel_form9_1 = view.findViewById(R.id.fuel_form9_1);
        fuel_form9_2 = view.findViewById(R.id.fuel_form9_2);
        fuel_form9_3 = view.findViewById(R.id.fuel_form9_3);
        fuel_form9_4 = view.findViewById(R.id.fuel_form9_4);
        fuel_form10_1 = view.findViewById(R.id.fuel_form10_1);
        fuel_form10_2 = view.findViewById(R.id.fuel_form10_2);
        fuel_form10_3 = view.findViewById(R.id.fuel_form10_3);
        fuel_form10_4 = view.findViewById(R.id.fuel_form10_4);
        fuel_form11_1 = view.findViewById(R.id.fuel_form11_1);
        fuel_form11_2 = view.findViewById(R.id.fuel_form11_2);
        fuel_form11_3 = view.findViewById(R.id.fuel_form11_3);
        fuel_form11_4 = view.findViewById(R.id.fuel_form11_4);
        fuel_form12_1 = view.findViewById(R.id.fuel_form12_1);
        fuel_form12_2 = view.findViewById(R.id.fuel_form12_2);
        fuel_form12_3 = view.findViewById(R.id.fuel_form12_3);
        fuel_form12_4 = view.findViewById(R.id.fuel_form12_4);
        fuel_form13_1 = view.findViewById(R.id.fuel_form13_1);
        fuel_form13_2 = view.findViewById(R.id.fuel_form13_2);
        fuel_form13_3 = view.findViewById(R.id.fuel_form13_3);
        fuel_form13_4 = view.findViewById(R.id.fuel_form13_4);
        fuel_form14_1 = view.findViewById(R.id.fuel_form14_1);
        fuel_form14_2 = view.findViewById(R.id.fuel_form14_2);
        fuel_form14_3 = view.findViewById(R.id.fuel_form14_3);
        fuel_form14_4 = view.findViewById(R.id.fuel_form14_4);
        fuel_form15_1 = view.findViewById(R.id.fuel_form15_1);
        fuel_form15_2 = view.findViewById(R.id.fuel_form15_2);
        fuel_form15_3 = view.findViewById(R.id.fuel_form15_3);
        fuel_form15_4 = view.findViewById(R.id.fuel_form15_4);
        fuel_form16_1 = view.findViewById(R.id.fuel_form16_1);
        fuel_form16_2 = view.findViewById(R.id.fuel_form16_2);
        fuel_form16_3 = view.findViewById(R.id.fuel_form16_3);
        fuel_form16_4 = view.findViewById(R.id.fuel_form16_4);
        fuel_form17_1 = view.findViewById(R.id.fuel_form17_1);
        fuel_form17_2 = view.findViewById(R.id.fuel_form17_2);
        fuel_form17_3 = view.findViewById(R.id.fuel_form17_3);
        fuel_form17_4 = view.findViewById(R.id.fuel_form17_4);
        fuel_form18_1 = view.findViewById(R.id.fuel_form18_1);
        fuel_form18_2 = view.findViewById(R.id.fuel_form18_2);
        fuel_form18_3 = view.findViewById(R.id.fuel_form18_3);
        fuel_form18_4 = view.findViewById(R.id.fuel_form18_4);
        fuel_form19_1 = view.findViewById(R.id.fuel_form19_1);
        fuel_form19_2 = view.findViewById(R.id.fuel_form19_2);
        fuel_form19_3 = view.findViewById(R.id.fuel_form19_3);
        fuel_form19_4 = view.findViewById(R.id.fuel_form19_4);
        fuel_form20_1 = view.findViewById(R.id.fuel_form20_1);
        fuel_form20_2 = view.findViewById(R.id.fuel_form20_2);
        fuel_form20_3 = view.findViewById(R.id.fuel_form20_3);
        fuel_form20_4 = view.findViewById(R.id.fuel_form20_4);
        fuel_form21_1 = view.findViewById(R.id.fuel_form21_1);
        fuel_form21_2 = view.findViewById(R.id.fuel_form21_2);
        fuel_form21_3 = view.findViewById(R.id.fuel_form21_3);
        fuel_form21_4 = view.findViewById(R.id.fuel_form21_4);
        fuel_form22_1 = view.findViewById(R.id.fuel_form22_1);
        fuel_form22_2 = view.findViewById(R.id.fuel_form22_2);
        fuel_form22_3 = view.findViewById(R.id.fuel_form22_3);
        fuel_form22_4 = view.findViewById(R.id.fuel_form22_4);
        fuel_form23_1 = view.findViewById(R.id.fuel_form23_1);
        fuel_form23_2 = view.findViewById(R.id.fuel_form23_2);
        fuel_form23_3 = view.findViewById(R.id.fuel_form23_3);
        fuel_form23_4 = view.findViewById(R.id.fuel_form23_4);
        fuel_form24_1 = view.findViewById(R.id.fuel_form24_1);
        fuel_form24_2 = view.findViewById(R.id.fuel_form24_2);
        fuel_form24_3 = view.findViewById(R.id.fuel_form24_3);
        fuel_form24_4 = view.findViewById(R.id.fuel_form24_4);
        fuel_form25_1 = view.findViewById(R.id.fuel_form25_1);
        fuel_form25_2 = view.findViewById(R.id.fuel_form25_2);
        fuel_form25_3 = view.findViewById(R.id.fuel_form25_3);
        fuel_form25_4 = view.findViewById(R.id.fuel_form25_4);
        fuel_form26_1 = view.findViewById(R.id.fuel_form26_1);
        fuel_form26_2 = view.findViewById(R.id.fuel_form26_2);
        fuel_form26_3 = view.findViewById(R.id.fuel_form26_3);
        fuel_form26_4 = view.findViewById(R.id.fuel_form26_4);
        fuel_form27_1 = view.findViewById(R.id.fuel_form27_1);
        fuel_form27_2 = view.findViewById(R.id.fuel_form27_2);
        fuel_form27_3 = view.findViewById(R.id.fuel_form27_3);
        fuel_form27_4 = view.findViewById(R.id.fuel_form27_4);
        fuel_form28_1 = view.findViewById(R.id.fuel_form28_1);
        fuel_form28_2 = view.findViewById(R.id.fuel_form28_2);
        fuel_form28_3 = view.findViewById(R.id.fuel_form28_3);
        fuel_form28_4 = view.findViewById(R.id.fuel_form28_4);

        ArrayList<EditText> fuel_form_3_list = new ArrayList<>();
        fuel_form_3_list.add(fuel_form1_3);
        fuel_form_3_list.add(fuel_form2_3);
        fuel_form_3_list.add(fuel_form3_3);
        fuel_form_3_list.add(fuel_form4_3);
        fuel_form_3_list.add(fuel_form5_3);
        fuel_form_3_list.add(fuel_form6_3);
        fuel_form_3_list.add(fuel_form7_3);
        fuel_form_3_list.add(fuel_form8_3);
        fuel_form_3_list.add(fuel_form9_3);
        fuel_form_3_list.add(fuel_form10_3);
        fuel_form_3_list.add(fuel_form11_3);
        fuel_form_3_list.add(fuel_form12_3);
        fuel_form_3_list.add(fuel_form13_3);
        fuel_form_3_list.add(fuel_form14_3);
        fuel_form_3_list.add(fuel_form15_3);
        fuel_form_3_list.add(fuel_form16_3);
        fuel_form_3_list.add(fuel_form17_3);
        fuel_form_3_list.add(fuel_form18_3);
        fuel_form_3_list.add(fuel_form19_3);
        fuel_form_3_list.add(fuel_form20_3);
        fuel_form_3_list.add(fuel_form21_3);
        fuel_form_3_list.add(fuel_form22_3);
        fuel_form_3_list.add(fuel_form23_3);
        fuel_form_3_list.add(fuel_form24_3);
        fuel_form_3_list.add(fuel_form25_3);
        fuel_form_3_list.add(fuel_form26_3);
        fuel_form_3_list.add(fuel_form27_3);
        fuel_form_3_list.add(fuel_form28_3);

        fuel_line2_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取当前日期
                Calendar calendar = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    calendar = Calendar.getInstance();
                }
                int year = 0;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    year = calendar.get(Calendar.YEAR);
                }
                int month = 0;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    month = calendar.get(Calendar.MONTH);
                }
                int day = 0;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    day = calendar.get(Calendar.DAY_OF_MONTH);
                }

                // 创建DatePickerDialog
                DatePickerDialog datePickerDialog = new DatePickerDialog(
                        getActivity(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                // 将选择的日期设置到EditText中
                                fuel_line2_1.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                            }
                        }, year, month, day);

                // 显示日期选择框
                datePickerDialog.show();
            }
        });
        buttonGeneratePDF = view.findViewById(R.id.buttonGeneratePDF);
        buttonGeneratePDF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // 传入Fragment的context
                    generateTempPDF(getContext());
                    Toast.makeText(getContext(), "PDF生成成功！", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        // 初始化预览按钮
        Button buttonPreviewPDF = view.findViewById(R.id.buttonPreviewPDF);
        buttonPreviewPDF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 调用预览PDF的方法
                previewPDF(getContext(), pdfFilePath);
            }
        });

        buttonSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                View signatureView = inflater.inflate(R.layout.layout_signature_pad, null);
                final SignaturePad signaturePad = signatureView.findViewById(R.id.signaturePad);

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setView(signatureView)
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 获取签名位图并保存到变量中
                                signatureBitmap = signaturePad.getSignatureBitmap();
                                // 将签名位图存储到本地
//                                saveSignatureToLocal(signatureBitmap);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                // 创建 AlertDialog 对象
                final AlertDialog dialog = builder.create();

                // 在弹窗显示后调整大小
                dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialogInterface) {
                        // 获取对话框窗口
                        Window window = dialog.getWindow();
                        if (window != null) {
                            // 获取窗口布局参数
                            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                            layoutParams.copyFrom(window.getAttributes());

                            // 设置弹窗宽度和高度（以像素为单位）
                            layoutParams.width = 1800;  // 宽度
                            layoutParams.height = 1000; // 高度

                            // 应用新的布局参数
                            window.setAttributes(layoutParams);
                        }
                    }
                });

                // 显示对话框
                dialog.show();
            }
        });

        // 设置点击事件，弹出选择框
        for (EditText editText : fuel_form_3_list) {
            if (editText != null) {  // 确保 EditText 存在
                // 为每个 EditText 设置点击事件
                editText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 创建弹出框
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("选择是否成功");

                        // 选项数组
                        String[] options = {"是", "否"};

                        builder.setItems(options, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 根据选择更新 EditText 内容
                                if (which == 0) {
                                    // 选择 "是"
                                    editText.setText("√");
                                } else if (which == 1) {
                                    // 选择 "否"
                                    editText.setText("×");
                                }
                            }
                        });

                        // 显示弹出框
                        builder.show();
                    }
                });
            }
        }

        return view;
    }

    // 修改generateTempPDF方法签名，传入Context
    public String generateTempPDF(Context context) throws Exception {
        PdfReader reader = null;
        PdfStamper ps = null;
        OutputStream fos = null;
        ByteArrayOutputStream bos = null;
        InputStream is = null;
        try {
            // 获取外部存储的根目录
            File externalStorage = Environment.getExternalStorageDirectory();

            // 构建具体的文件路径
            File file = new File(externalStorage, "MFBdata/Template/FuelCardTemplatePDF.pdf");

            // 检查文件是否存在
            if (file.exists()) {
                // 如果文件存在，使用 FileInputStream 读取文件
                is = new FileInputStream(file);
                Log.d("FileSuccess", "模板文件已找到: " + file.getAbsolutePath());
            } else {
                // 如果文件不存在，提示未发现模板文件
                Log.e("FileError", "未发现模板文件: " + file.getAbsolutePath());
                Toast.makeText(context, "未发现模板文件", Toast.LENGTH_SHORT).show();
            }

            reader = new PdfReader(is);
            bos = new ByteArrayOutputStream();
            ps = new PdfStamper(reader, bos);

            // 使用中文字体
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            ArrayList<BaseFont> fontList = new ArrayList<>();
            fontList.add(bf);

            AcroFields fields = ps.getAcroFields();
            fields.setSubstitutionFonts(fontList);
            // 设置表单字段字体大小及其他样式
            for (String fieldName : fields.getFields().keySet()) {
                fields.setFieldProperty(fieldName, "textfont", bf, null);  // 设置中文字体
                fields.setFieldProperty(fieldName, "textsize", 12f, null);  // 设置字体大小为12
            }

            fields.setSubstitutionFonts(fontList);
            fillData(fields, data()); // 渲染数据

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            signatureBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            Image signatureImage = Image.getInstance(stream.toByteArray());
            Rectangle signRect = fields.getFieldPositions("fuel_line1_3_af_image").get(0).position;

            float x = signRect.getLeft();
            float y = signRect.getBottom();

            float width = signRect.getWidth();
            float height = signRect.getHeight();

            // 调整图像大小
            signatureImage.scaleToFit(width-1, height+10);
            signatureImage.setAbsolutePosition(x, y + 5);

            // 在PDF中添加图像
            ps.getOverContent(1).addImage(signatureImage);
            // 必须调用这个，否则文档不会生成
            ps.setFormFlattening(true);
            ps.close();

            // 获取当前日期作为文件夹名称
            String currentDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());

            // 获取当前时间戳作为文件名
            String timeStamp = String.valueOf(System.currentTimeMillis());

            // 构建你要保存的目录路径
            File dir = new File(externalStorage, "MFBdata/FuelCard");
            if (!dir.exists()) {
                dir.mkdirs();  // 创建日期文件夹
            }

            // 生成 PDF 文件的路径
            pdfFilePath = dir.getAbsolutePath() + "/" + timeStamp + ".pdf";

            // 写入 PDF 文件
            fos = new FileOutputStream(pdfFilePath);
            fos.write(bos.toByteArray());

            Log.d("PDFGeneration", "PDF 生成成功: " + pdfFilePath);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (reader != null) {
                reader.close();
            }
            if (is != null) {
                is.close();
            }
        }

        return pdfFilePath;  // 返回生成的 PDF 文件路径
    }

    /**
     * 填充模板中的数据
     */
    public void fillData(AcroFields fields, Map<String, String> data) {
        try {
            for (String key : data.keySet()) {
                String value = data.get(key);
                // 为字段赋值,注意字段名称是区分大小写的
                fields.setField(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 填充数据源
     * 其中data存放的key值与pdf模板中的文本域值相对应
     */
    public Map<String, String> data() {
        Map<String, String> data = new HashMap<String, String>();
        //key要与模板中的别名一一对应
        data.put("fuel_line1_1", fuel_line1_1.getText().toString());
        data.put("fuel_line1_2", fuel_line1_2.getText().toString());
//        data.put("fuel_line1_3", fuel_line1_3.getText().toString());
        data.put("fuel_line2_1", fuel_line2_1.getText().toString());
        data.put("fuel_line2_2", fuel_line2_2.getText().toString());
        data.put("fuel_line2_3", fuel_line2_3.getText().toString());
        data.put("fuel_line3_1", fuel_line3_1.getText().toString());
        data.put("fuel_line3_2", fuel_line3_2.getText().toString());
        data.put("fuel_line3_3", fuel_line3_3.getText().toString());
        data.put("fuel_line3_4", fuel_line3_4.getText().toString());
        data.put("fuel_line3_5", fuel_line3_5.getText().toString());
        data.put("fuel_line3_6", fuel_line3_6.getText().toString());
        data.put("fuel_line4_1", fuel_line4_1.getText().toString());
        data.put("fuel_line4_2", fuel_line4_2.getText().toString());
        data.put("fuel_line4_3", fuel_line4_3.getText().toString());
        data.put("fuel_line4_4", fuel_line4_4.getText().toString());
        data.put("fuel_line4_5", fuel_line4_5.getText().toString());
        data.put("fuel_line4_6", fuel_line4_6.getText().toString());

        data.put("fuel_form1_1", fuel_form1_1.getText().toString());
        data.put("fuel_form1_2", fuel_form1_2.getText().toString());
        data.put("fuel_form1_3", fuel_form1_3.getText().toString());
        data.put("fuel_form1_4", fuel_form1_4.getText().toString());
        data.put("fuel_form2_1", fuel_form2_1.getText().toString());
        data.put("fuel_form2_2", fuel_form2_2.getText().toString());
        data.put("fuel_form2_3", fuel_form2_3.getText().toString());
        data.put("fuel_form2_4", fuel_form2_4.getText().toString());
        data.put("fuel_form3_1", fuel_form3_1.getText().toString());
        data.put("fuel_form3_2", fuel_form3_2.getText().toString());
        data.put("fuel_form3_3", fuel_form3_3.getText().toString());
        data.put("fuel_form3_4", fuel_form3_4.getText().toString());
        data.put("fuel_form4_1", fuel_form4_1.getText().toString());
        data.put("fuel_form4_2", fuel_form4_2.getText().toString());
        data.put("fuel_form4_3", fuel_form4_3.getText().toString());
        data.put("fuel_form4_4", fuel_form4_4.getText().toString());
        data.put("fuel_form5_1", fuel_form5_1.getText().toString());
        data.put("fuel_form5_2", fuel_form5_2.getText().toString());
        data.put("fuel_form5_3", fuel_form5_3.getText().toString());
        data.put("fuel_form5_4", fuel_form5_4.getText().toString());
        data.put("fuel_form6_1", fuel_form6_1.getText().toString());
        data.put("fuel_form6_2", fuel_form6_2.getText().toString());
        data.put("fuel_form6_3", fuel_form6_3.getText().toString());
        data.put("fuel_form6_4", fuel_form6_4.getText().toString());
        data.put("fuel_form7_1", fuel_form7_1.getText().toString());
        data.put("fuel_form7_2", fuel_form7_2.getText().toString());
        data.put("fuel_form7_3", fuel_form7_3.getText().toString());
        data.put("fuel_form7_4", fuel_form7_4.getText().toString());
        data.put("fuel_form8_1", fuel_form8_1.getText().toString());
        data.put("fuel_form8_2", fuel_form8_2.getText().toString());
        data.put("fuel_form8_3", fuel_form8_3.getText().toString());
        data.put("fuel_form8_4", fuel_form8_4.getText().toString());
        data.put("fuel_form9_1", fuel_form9_1.getText().toString());
        data.put("fuel_form9_2", fuel_form9_2.getText().toString());
        data.put("fuel_form9_3", fuel_form9_3.getText().toString());
        data.put("fuel_form9_4", fuel_form9_4.getText().toString());
        data.put("fuel_form10_1", fuel_form10_1.getText().toString());
        data.put("fuel_form10_2", fuel_form10_2.getText().toString());
        data.put("fuel_form10_3", fuel_form10_3.getText().toString());
        data.put("fuel_form10_4", fuel_form10_4.getText().toString());
        data.put("fuel_form11_1", fuel_form11_1.getText().toString());
        data.put("fuel_form11_2", fuel_form11_2.getText().toString());
        data.put("fuel_form11_3", fuel_form11_3.getText().toString());
        data.put("fuel_form11_4", fuel_form11_4.getText().toString());
        data.put("fuel_form12_1", fuel_form12_1.getText().toString());
        data.put("fuel_form12_2", fuel_form12_2.getText().toString());
        data.put("fuel_form12_3", fuel_form12_3.getText().toString());
        data.put("fuel_form12_4", fuel_form12_4.getText().toString());
        data.put("fuel_form13_1", fuel_form13_1.getText().toString());
        data.put("fuel_form13_2", fuel_form13_2.getText().toString());
        data.put("fuel_form13_3", fuel_form13_3.getText().toString());
        data.put("fuel_form13_4", fuel_form13_4.getText().toString());
        data.put("fuel_form14_1", fuel_form14_1.getText().toString());
        data.put("fuel_form14_2", fuel_form14_2.getText().toString());
        data.put("fuel_form14_3", fuel_form14_3.getText().toString());
        data.put("fuel_form14_4", fuel_form14_4.getText().toString());
        data.put("fuel_form15_1", fuel_form15_1.getText().toString());
        data.put("fuel_form15_2", fuel_form15_2.getText().toString());
        data.put("fuel_form15_3", fuel_form15_3.getText().toString());
        data.put("fuel_form15_4", fuel_form15_4.getText().toString());
        data.put("fuel_form16_1", fuel_form16_1.getText().toString());
        data.put("fuel_form16_2", fuel_form16_2.getText().toString());
        data.put("fuel_form16_3", fuel_form16_3.getText().toString());
        data.put("fuel_form16_4", fuel_form16_4.getText().toString());
        data.put("fuel_form17_1", fuel_form17_1.getText().toString());
        data.put("fuel_form17_2", fuel_form17_2.getText().toString());
        data.put("fuel_form17_3", fuel_form17_3.getText().toString());
        data.put("fuel_form17_4", fuel_form17_4.getText().toString());
        data.put("fuel_form18_1", fuel_form18_1.getText().toString());
        data.put("fuel_form18_2", fuel_form18_2.getText().toString());
        data.put("fuel_form18_3", fuel_form18_3.getText().toString());
        data.put("fuel_form18_4", fuel_form18_4.getText().toString());
        data.put("fuel_form19_1", fuel_form19_1.getText().toString());
        data.put("fuel_form19_2", fuel_form19_2.getText().toString());
        data.put("fuel_form19_3", fuel_form19_3.getText().toString());
        data.put("fuel_form19_4", fuel_form19_4.getText().toString());
        data.put("fuel_form20_1", fuel_form20_1.getText().toString());
        data.put("fuel_form20_2", fuel_form20_2.getText().toString());
        data.put("fuel_form20_3", fuel_form20_3.getText().toString());
        data.put("fuel_form20_4", fuel_form20_4.getText().toString());
        data.put("fuel_form21_1", fuel_form21_1.getText().toString());
        data.put("fuel_form21_2", fuel_form21_2.getText().toString());
        data.put("fuel_form21_3", fuel_form21_3.getText().toString());
        data.put("fuel_form21_4", fuel_form21_4.getText().toString());
        data.put("fuel_form22_1", fuel_form22_1.getText().toString());
        data.put("fuel_form22_2", fuel_form22_2.getText().toString());
        data.put("fuel_form22_3", fuel_form22_3.getText().toString());
        data.put("fuel_form22_4", fuel_form22_4.getText().toString());
        data.put("fuel_form23_1", fuel_form23_1.getText().toString());
        data.put("fuel_form23_2", fuel_form23_2.getText().toString());
        data.put("fuel_form23_3", fuel_form23_3.getText().toString());
        data.put("fuel_form23_4", fuel_form23_4.getText().toString());
        data.put("fuel_form24_1", fuel_form24_1.getText().toString());
        data.put("fuel_form24_2", fuel_form24_2.getText().toString());
        data.put("fuel_form24_3", fuel_form24_3.getText().toString());
        data.put("fuel_form24_4", fuel_form24_4.getText().toString());
        data.put("fuel_form25_1", fuel_form25_1.getText().toString());
        data.put("fuel_form25_2", fuel_form25_2.getText().toString());
        data.put("fuel_form25_3", fuel_form25_3.getText().toString());
        data.put("fuel_form25_4", fuel_form25_4.getText().toString());
        data.put("fuel_form26_1", fuel_form26_1.getText().toString());
        data.put("fuel_form26_2", fuel_form26_2.getText().toString());
        data.put("fuel_form26_3", fuel_form26_3.getText().toString());
        data.put("fuel_form26_4", fuel_form26_4.getText().toString());
        data.put("fuel_form27_1", fuel_form27_1.getText().toString());
        data.put("fuel_form27_2", fuel_form27_2.getText().toString());
        data.put("fuel_form27_3", fuel_form27_3.getText().toString());
        data.put("fuel_form27_4", fuel_form27_4.getText().toString());
        data.put("fuel_form28_1", fuel_form28_1.getText().toString());
        data.put("fuel_form28_2", fuel_form28_2.getText().toString());
        data.put("fuel_form28_3", fuel_form28_3.getText().toString());
        data.put("fuel_form28_4", fuel_form28_4.getText().toString());

        return data;
    }

    // 添加预览PDF的方法
    public void previewPDF(Context context, String pdfFilePath) {
        File pdfFile = new File(pdfFilePath);  // 传入最新生成的文件路径
        if (pdfFile.exists()) {
            Uri pdfUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", pdfFile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(pdfUri, "application/pdf");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, "没有找到可以打开 PDF 的应用", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "PDF 文件不存在，请先生成 PDF", Toast.LENGTH_SHORT).show();
        }
    }

    private void saveSignatureToLocal(Bitmap signatureBitmap) {
        FileOutputStream outputStream = null;
        try {
            // 创建文件名和路径
            File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Signatures");
            if (!directory.exists()) {
                directory.mkdirs(); // 创建存储目录
            }
            String fileName = "signature_" + System.currentTimeMillis() + ".png";
            File signatureFile = new File(directory, fileName);

            // 打开文件输出流
            outputStream = new FileOutputStream(signatureFile);

            // 将位图压缩为PNG并写入文件
            signatureBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

            // 刷新文件输出流，确保所有数据被写入
            outputStream.flush();

            // 提示用户保存成功
            Toast.makeText(this.getContext(), "签名已保存至: " + signatureFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this.getContext(), "保存签名失败", Toast.LENGTH_LONG).show();
        } finally {
            // 关闭输出流
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}